<?php

/**
 * Description of Profil
 *
 * @author ROCU Fabien
 */
class Profil {
    public $profil_id;
    public $profil_nom;
    public $profil_droit;

    public $profil_droits = array(
            "dieu" => false,
            "disponibilitees" => false,
            "retenues" => false,
            "mutualisation" => false,
            "epi" => false,
            "groupement" => false,
            "personne" => false,
            "utilisateur" => false,
            "pompier" => false,
            "parametrage" => false,
            "centre" => false,
            "multiple_centre" => false,
            "multiple_pompier" => false
    );

    public $profil_droits_parametres = array(
            "dieu" => false,
            "disponibilitees" => false,
            "retenues" => false,
            "mutualisation" => false,
            "epi" => false,
            "groupement" => false,
            "personne" => false,
            "utilisateur" => false,
            "pompier" => false,
            "parametrage" => false,
            "centre" => false,
            "multiple_centre" => false,
            "multiple_pompier" => false
    );

    public function creer_profil(  ) // INSERT
    {
        global $_mysql;

        try {
            $query = "INSERT INTO profil VALUES ( null, '$this->profil_nom', '$this->profil_droit');";
            if ( Config::$debugSQL ) echo $query;
            $result = $_mysql->query($query);

            if ( $result ) {
                $this->profil_id = $_mysql->last_insert_id();
                return $this->profil_id;
            } else
                throw new Exception('Erreur Profil: la création du profil a rencontré un problème');

        }
        catch (SQLException $sqle) {
            throw $sqle;
        }
    }

    public function charger_profil( $id ) // SELECT
    {
        global $_mysql;

        $this->personne_id = $id;

        try {
            $query = "SELECT * FROM profil WHERE profil_id = $id";
            if ( Config::$debugSQL ) echo "<br />" . $query . "<br />";
            $result = $_mysql->query($query);

            if ( $_mysql->num_rows($result) > 0 ) {
                $val = $_mysql->fetch_array($result);

                $this->profil_nom = $val['profil_nom'];
                $this->profil_droit = $val['profil_droit'];

                $droits = explode("@", $this->profil_droit);

                for ( $i=0 ; $i < count($droits) ; $i++ ) {
                    $tmp = explode(":", $droits[$i]);
                    $this->profil_droits[$tmp[0]] = true;
                    if ( count($tmp) != 1 ) $this->profil_droits_parametres[$tmp[0]] = $tmp[1];
                }

                if ( Config::$debugWHO_IS ) echo $this->qui_suis_je();
            }
            else
                throw new Exception('Erreur Profil: identifiant unique non respecté');

        }
        catch (SQLException $sqle) {
            throw $sqle;
        }

    }

    public function set_profil( $nom, $droit ) {
        $this->profil_nom = $nom;
        $this->profil_droit = $droit;

        if ( Config::$debugWHO_IS ) echo $this->qui_suis_je();
    }

    public function sauvegarder_profil() // UPDATE
    {
        global $_mysql;

        try {
            $query = "UPDATE profil SET profil_nom = '" . $this->profil_nom . "', profil_droit = '" . $this->profil_droit . "' WHERE profil_id = " . $this->profil_id . ";";
            if ( Config::$debugSQL ) echo $query;
            $result = $_mysql->query($query);

            if ( $result ) {

            } // Tout se passe bien ne rien faire
            else
                throw new Exception('Erreur Profil: la modification du profil a rencontré un problème');

        }
        catch (SQLException $sqle) {
            throw $sqle;
        }
    }

    public function effacer_profil( $id ) // DELETE
    {
        $profil_id = $id;

        global $_mysql;

        try {
            $query = "DELETE FROM profil WHERE profil_id = $profil_id";
            if ( Config::$debugSQL ) echo $query;
            $result = $_mysql->query($query);

            if ( $result ) {

            } // Tout se passe bien ne rien faire
            else
                throw new Exception('Erreur Profil: la suppression du profil a rencontré un problème');

        }
        catch (SQLException $sqle) {
            throw $sqle;
        }
    }

    public function qui_suis_je() // Return String
    {
        return " <br /> Profil --> ID: " . $this->profil_id . " Nom: " . $this->profil_nom . " Droit: " . $this->profil_droit . "<br />";
    }
}

?>
